﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using PX.File;

public partial class Query : System.Web.UI.Page
{
	private FileComponent clsFileComp = new FileComponent();
	private string strName = "";

	protected override void OnInit(EventArgs e)
	{
		base.OnInit(e);

		lBtnFirst.Click += new EventHandler(lBtnFirst_Click);
		lBtnForware.Click += new EventHandler(lBtnForware_Click);
		lBtnNext.Click += new EventHandler(lBtnNext_Click);
		lBtnLast.Click += new EventHandler(lBtnLast_Click);

		grvFiles.DataBound += new EventHandler(grvFiles_DataBound);
		grvFiles.RowDataBound += new GridViewRowEventHandler(grvFiles_RowDataBound);
	}

	protected void Page_Load(object sender, EventArgs e)
	{
		
		strName = Request.QueryString["tex_search"].ToString();
		BindGridView(strName, grvFiles);
	}

	#region GridView相关

	////////////////////////////////////////////////////////////////////////////////////////////////////
	//
	void grvFiles_DataBound(object sender, EventArgs e)
	{
		if (grvFiles.PageIndex == 0)
		{
			lBtnForware.Enabled = false;
		}
		else
		{
			lBtnForware.Enabled = true;
		}

		if (grvFiles.PageIndex == grvFiles.PageCount - 1)
		{
			lBtnNext.Enabled = false;
		}
		else
		{
			lBtnNext.Enabled = true;
		}
	}
	////////////////////////////////////////////////////////////////////////////////////////////////////

	////////////////////////////////////////////////////////////////////////////////////////////////////
	//
	void grvFiles_RowDataBound(object sender, GridViewRowEventArgs e)
	{
		int nFileID = 0;

		if (e.Row.RowIndex >= 0)
		{
			PX.File.File clsFile = new File();

			nFileID = Convert.ToInt32(grvFiles.DataKeys[e.Row.RowIndex].Value);
			clsFile = (PX.File.File)clsFileComp.GetOneByID(nFileID);

			((HyperLink)e.Row.FindControl("aFile")).NavigateUrl = "Content.aspx?FileID=" + clsFile.ID.ToString();
		}
	}
	////////////////////////////////////////////////////////////////////////////////////////////////////

	#endregion GridView相关

	#region 翻页

	////////////////////////////////////////////////////////////////////////////////////////////////////
	//
	/// <summary>
	/// 翻到第一页
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	void lBtnFirst_Click(object sender, EventArgs e)
	{
		grvFiles.PageIndex = 0;
		BindGridView(strName, grvFiles);
	}
	////////////////////////////////////////////////////////////////////////////////////////////////////

	////////////////////////////////////////////////////////////////////////////////////////////////////
	//
	/// <summary>
	/// 翻到上一页
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	void lBtnForware_Click(object sender, EventArgs e)
	{
		grvFiles.PageIndex = grvFiles.PageIndex - 1;
		BindGridView(strName, grvFiles);
	}
	////////////////////////////////////////////////////////////////////////////////////////////////////

	////////////////////////////////////////////////////////////////////////////////////////////////////
	//
	/// <summary>
	/// 翻到下一页
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	void lBtnNext_Click(object sender, EventArgs e)
	{
		grvFiles.PageIndex = grvFiles.PageIndex + 1;
		BindGridView(strName, grvFiles);
	}
	////////////////////////////////////////////////////////////////////////////////////////////////////

	////////////////////////////////////////////////////////////////////////////////////////////////////
	//
	/// <summary>
	/// 翻到最后一页
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	void lBtnLast_Click(object sender, EventArgs e)
	{
		grvFiles.PageIndex = grvFiles.PageCount - 1;
		BindGridView(strName, grvFiles);
	}
	////////////////////////////////////////////////////////////////////////////////////////////////////

	#endregion 翻页

	private void BindGridView(string strName, GridView gv)
	{
		FileQuery clsQuery = new FileQuery();
		

		//clsQuery.nColumnID = nColumnID;
		clsQuery.strFileName = strName;

		gv.DataSource = clsFileComp.Query(clsQuery);
		gv.DataKeyNames = new string[] { "ID" };
		gv.DataBind();
	}
}
